<?php
namespace app\common\model;

use Carbon\Carbon;

class Activity extends Base
{
    protected function getStatusAttr($value, $data)
    {
        $date = Carbon::today();
        return ($date > $data['end_time']) ? 0:1;
    }

    protected function getStartTimeAttr($value, $data)
    {
        return date('Y-m-d',strtotime($data['start_time']));
    }

    protected function getEndTimeAttr($value, $data)
    {
        return date('Y-m-d',strtotime($data['end_time']));
    }

    public function saveChange($data){
        $data['update_time'] = time();
        if(is_numeric($data['id'])){
            return $this->validate(true)->allowField(true)->save($data,[$this->getPk()=>$data['id']]);
        }else{
            $data['create_time'] = time();
            return $this->validate(true)->allowField(true)->save($data);
        }
    }
    
    public function getList($map = []){
        return self::where($map)
            ->order('sort')
            ->paginate('',false,['query'=>request()->param()]);
    }

    public function getActivity($map = [])
    {
        return self::where($map)->order('sort')->select();
    }
}